/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Interface;

import Controladores.MedicalrecordJpaController;
import Controladores.VacineJpaController;
import Controladores.exceptions.PreexistingEntityException;
import entidades.Medicalrecord;
import entidades.Vacine;
import java.awt.Dimension;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.swing.JOptionPane;

/**
 *
 * @author Gatoper
 */
public class VPIVacine extends javax.swing.JPanel {

    /**
     * Creates new form VPIVacine
     */
    public VPIVacine() {
        initComponents();
        this.setSize(new Dimension(300, 400));
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        LID = new javax.swing.JLabel();
        TID = new javax.swing.JTextField();
        Tdosage = new javax.swing.JTextField();
        Ldosage = new javax.swing.JLabel();
        TName = new javax.swing.JTextField();
        LName = new javax.swing.JLabel();
        Lmedicalrecord = new javax.swing.JLabel();
        CBmedicalrecord = new javax.swing.JComboBox();
        Bcrear = new javax.swing.JButton();

        LID.setText("ID:");

        TID.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                TIDActionPerformed(evt);
            }
        });

        Tdosage.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                TdosageActionPerformed(evt);
            }
        });

        Ldosage.setText("Dosage:");

        TName.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                TNameActionPerformed(evt);
            }
        });

        LName.setText("Name:");

        Lmedicalrecord.setText("Medical Record:");

        EntityManagerFactory emf;
        EntityManager em;
        Map<String, String> properties=new HashMap<>();
        properties.put("javax.persistence.jdbc:user", "root");
        properties.put("javax.persistence.jdbc:password", "");
        emf=Persistence.createEntityManagerFactory("HPVAS",properties);
        em=emf.createEntityManager();
        Medicalrecord medicalrecord=new Medicalrecord();
        MedicalrecordJpaController mjc=new MedicalrecordJpaController(emf);
        List l1=mjc.findMedicalrecordEntities();
        String [] aux1=new String[l1.size()];
        for(int i=0;i<l1.size();i++)
        aux1[i]=((Medicalrecord)l1.get(i)).getId()+"";
        CBmedicalrecord.setModel(new javax.swing.DefaultComboBoxModel(aux1));

        Bcrear.setText("Create");
        Bcrear.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                BcrearActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(Bcrear)
                            .addComponent(Lmedicalrecord))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(CBmedicalrecord, 0, 96, Short.MAX_VALUE))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(LID)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(TID))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(LName)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(TName))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(Ldosage)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(Tdosage)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(LID)
                    .addComponent(TID, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(Ldosage)
                    .addComponent(Tdosage, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(LName)
                    .addComponent(TName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(Lmedicalrecord)
                    .addComponent(CBmedicalrecord, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(Bcrear)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void TIDActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_TIDActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_TIDActionPerformed

    private void TdosageActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_TdosageActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_TdosageActionPerformed

    private void TNameActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_TNameActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_TNameActionPerformed

    private void BcrearActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_BcrearActionPerformed
        // TODO add your handling code here:
        boolean flag = true;
        if (TID.getText().length() < 1) {
            flag = false;
        }
        if (TName.getText().length() < 1) {
            flag = false;
        }
        if (Tdosage.getText().length() < 1) {
            flag = false;
        }
        if (flag) {
            EntityManagerFactory emf;
            EntityManager em;
            Map<String, String> properties = new HashMap<>();
            emf = Persistence.createEntityManagerFactory("HPVAS");
            em = emf.createEntityManager();
            MedicalrecordJpaController mjc = new MedicalrecordJpaController(emf);
            Medicalrecord m = mjc.findMedicalrecord(Long.parseLong(this.CBmedicalrecord.getSelectedItem().toString()));
            Vacine v = new Vacine(this.TName.getText(), Float.parseFloat(this.Tdosage.getText()), m, Long.parseLong(this.TID.getText()));
            VacineJpaController vjc = new VacineJpaController(emf);
            try {
                vjc.create(v);
                JOptionPane.showMessageDialog(this, "Exito");
            } catch (PreexistingEntityException ex) {
                Logger.getLogger(VPIVacine.class.getName()).log(Level.SEVERE, null, ex);
            } catch (Exception ex) {
                Logger.getLogger(VPIVacine.class.getName()).log(Level.SEVERE, null, ex);
            }
        } else {
            JOptionPane.showMessageDialog(this, "Por favor llene todos los campos");
        }
    }//GEN-LAST:event_BcrearActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton Bcrear;
    private javax.swing.JComboBox CBmedicalrecord;
    private javax.swing.JLabel LID;
    private javax.swing.JLabel LName;
    private javax.swing.JLabel Ldosage;
    private javax.swing.JLabel Lmedicalrecord;
    private javax.swing.JTextField TID;
    private javax.swing.JTextField TName;
    private javax.swing.JTextField Tdosage;
    // End of variables declaration//GEN-END:variables
}
